package com.jiai.cloud.sys.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.TOrder;
import com.jiai.cloud.sys.service.ITOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2017-05-24
 */
@RestController

@RequestMapping("/api/v1/order")
public class TOrderController {

    @Autowired
    private ITOrderService itOrderService;
<<<<<<< Updated upstream

    //查询所有的
    @RequestMapping(value = "/findByAll/{page}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object findByAll(@PathVariable("page" ) Integer page1) throws ServiceException {
        try {
            Page<TOrder> page = new Page<>(page1, 20);
            itOrderService.selectPage(page, null);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    //插入
    @RequestMapping(value = "/insert", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object addTOrder(@RequestBody TOrder tOrder) throws ServiceException {
        try {
            itOrderService.insert(tOrder);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("插入失敗", e.getMessage());
        }
    }


    //根据订单查找
    @RequestMapping(value = "/findById/{orderno}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findById(@PathVariable("orderno") String orderno) throws ServiceException {
        try {
            itOrderService.selectById(orderno);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    @RequestMapping(value = "/del/{orderno}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
=======

    //增
    @RequestMapping(value = "/addOrder", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object addOrder(@RequestBody TOrder tOrder) throws ServiceException {
        try {
            itOrderService.insert(tOrder);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("插入失敗", e.getMessage());
        }
    }

    //删
    @RequestMapping(value = "/del/{orderno}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
>>>>>>> Stashed changes
    @ResponseBody
    public Object deleteId(@PathVariable("orderno") String orderno) throws ServiceException {
        try {
            itOrderService.deleteById(orderno);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

<<<<<<< Updated upstream
=======
    //查询所有的
    @RequestMapping(value = "/findByAll", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object findByAll() throws ServiceException {
        try {
            Page<TOrder> page = new Page<>(1, 20);
            itOrderService.selectPage(page, null);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    //根据订单查找
    @RequestMapping(value = "/findById/{orderno}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findById(@PathVariable("orderno") String orderno) throws ServiceException {
        try {
            itOrderService.selectById(orderno);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }


    //改(有就改，没有就插入)
>>>>>>> Stashed changes
    @RequestMapping(value = "/edit", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object edit(@RequestBody TOrder tOrder) throws ServiceException {
        try {
            itOrderService.insertOrUpdate(tOrder);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("编辑失敗", e.getMessage());
        }
    }

}
